package thread.pcProblem;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class BlockingQueueExample {

    public static void main(String[] args) throws Exception {

        BlockingQueue  q1 = new ArrayBlockingQueue(1024);
        BlockingQueue  q2 = new DelayQueue();
        BlockingQueue  q3 = new LinkedBlockingDeque ();

        Producer producer = new Producer(q1);
        Consumer consumer = new Consumer(q1);

        new Thread(producer).start();
        new Thread(consumer).start();

        System.out.println("here 1 ");
        Thread.sleep(4000);
        System.out.println("here 2 ");
    }
}


// producer class ------
class Producer implements Runnable{

    protected BlockingQueue queue = null;

    public Producer(BlockingQueue queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            queue.put("1");
            Thread.sleep(1000);
            queue.put("2");
            Thread.sleep(1000);
            queue.put("3");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

// consumer class -------
class Consumer implements Runnable {

    protected BlockingQueue queue = null;

    public Consumer(BlockingQueue queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            System.out.println("Consumer "+queue.take());
            System.out.println("Consumer "+queue.take());
            System.out.println("Consumer "+queue.take());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}